﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using KPIS.GERP.Models.HRM;

namespace KPIS.GERP.Models.Class
{
    public class AbsenceUtility
    {
        public IEnumerable<DateTime> GetRegularWorkingDayList(DateTime start, DateTime end)
        {
            var nonWorkingDays = NonworkingDates(new ThaiBuddhistCalendar().GetYear(DateTime.Now));
            return Range(start, end)
                .Where(x => x.DayOfWeek != DayOfWeek.Sunday)
                .Where(x => x.DayOfWeek != DayOfWeek.Saturday)
                .Where(x => !nonWorkingDays.Contains(x.Date));
        }

        public IEnumerable<DateTime> NonworkingDates(int bgmYear)
        {
            var db = new GERPContext();
            if (lst == null)
            {
                lst = db.PsNonWorkingDays.Where(o => o.BgYear == bgmYear && o.RecordStatus != "D").ToList();

                //new DAL.NonWorkingDays().GetAllNonWorkingDays(
                //    (new ThaiBuddhistCalendar().GetYear(DateTime.Now)).ToString());
            }

            return from nwd in lst select nwd.NonWorkingDate;
        } 

        public int GetRegularWorkingDays(DateTime start, DateTime end)
        {
            // ตรวจสอบ เสาร์ อาทิตย์ และวันหยุด .Where(x => CheckHoliday(x.ToShortDateString()) == 0)//
            return Range(start, end)
                .Where(x => x.DayOfWeek != DayOfWeek.Sunday)
                .Where(x => x.DayOfWeek != DayOfWeek.Saturday)
                .Count(x => CheckHoliday(x.ToShortDateString()) == 0);
         }

        public int GetRegularAllDays(DateTime start, DateTime end)
        {
            return (from day in Range(start, end) select day).Count();
        }

        //public static int cnt;
        static List<PsNonWorkingDay> lst;
        private int CheckHoliday(string d)
        {
            var db = new GERPContext();
            if (lst == null)
            {
                var bgm_year = new ThaiBuddhistCalendar().GetYear(DateTime.Now);
                lst = db.PsNonWorkingDays.Where(o => o.BgYear == bgm_year).ToList();

                //new DAL.NonWorkingDays().GetAllNonWorkingDays(
                //    (new ThaiBuddhistCalendar().GetYear(DateTime.Now)).ToString());
            }
            var c = from item in lst
                    where item.NonWorkingDate.ToShortDateString().Equals(d)
                    select lst;
            return c.Count() ;
        }
        
        IEnumerable<DateTime> Range(DateTime start, DateTime end)
        {
            if (start <= end)
            {
                while (start <= end)
                {
                    yield return start;
                    start = start.AddDays(1);
                }
            }
            else
            {
                while (end <= start)
                {
                    yield return start;
                    start = start.AddDays(-1);
                }
            }
        }
    }
}
